package asionreachel.cn.smarthomedemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import asionreachel.cn.smarthomedemo.R;
import asionreachel.cn.smarthomedemo.base.BaseActivity;
import asionreachel.cn.smarthomedemo.base.Common;
import asionreachel.cn.smarthomedemo.widget.filter.FilterImageView;
import familyclient.android.configer.JApConfiger;

/**
 * Created by SuperMa on 2017/6/24.
 */
public class RedeployActivity extends BaseActivity {
    private final String FUNCTION = "ApConfig";

    private String account;
    private String pass;

    private FilterImageView fivSet;

    private JApConfiger m_configerAp = new JApConfiger();

    private JApConfiger.JApConfigerListener m_listenerApConfiger = new JApConfiger.JApConfigerListener() {
        @Override
        public void ApHWConfigDeviceCb(String a_strDeviceID, String a_strDeviceIP, String a_strErr) {
            Common.closeDialogLoading();
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
            else
            {
                Common.showToast( "配置成功" );
                setResult( RESULT_OK );
                finish();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        account = getIntent().getStringExtra("account");
        pass = getIntent().getStringExtra("pass");

        setContentView( R.layout.activity_redeploy_wifi );

        m_configerAp.SetListener( m_listenerApConfiger );

        ImageView ivBack = (ImageView)findViewById(R.id.iv_back);
        Button btnYes = (Button)findViewById(R.id.fill_btn_yes);
        fivSet = (FilterImageView) findViewById(R.id.redeploy_fiv_into_set);

        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBack();
            }
        });

        fivSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent wifiSettingsIntent = new Intent("android.settings.WIFI_SETTINGS");
                startActivity(wifiSettingsIntent);
            }
        });

        btnYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //已经选择好wifi进行下一步
                if( m_configerAp.ApHWConfigDevice( Common.getWifiName(), account, pass, Common.s_strFamilyID ) )
                    Common.showDialogLoading( RedeployActivity.this , "正在配置...");
                else
                    Common.showToast( "无法配置，请检查热点连接" );
            }
        });

        fivSet.onStart();
    }

    public void onBack() {
        mActivity.finish();
    }

    @Override
    public void onBackKey() {
        onBack();
    }

    protected void onDestroy() {
        m_configerAp.SetListener( null );
        fivSet.onStop();
        super.onDestroy();
    }
}